package Guicauhoi.backing;

import Guicauhoi.GuicauhoiPortlet;

import Guicauhoi.resource.GuicauhoiPortletBundle;


import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;

import javax.faces.event.ValueChangeEvent;

import nl.captcha.Captcha;

import javax.portlet.ActionResponse;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import common.Utils;

import java.util.List;
import java.util.Locale;

import oracle.adf.model.binding.DCIteratorBinding;

import java.util.ResourceBundle;

import javax.faces.model.SelectItem;

import javax.servlet.http.HttpServletRequest;

import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;

import oracle.adf.view.rich.component.rich.layout.RichPanelFormLayout;
import oracle.adf.view.rich.component.rich.output.RichOutputText;

import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;
import oracle.jbo.server.ViewObjectImpl;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;

public class GuicauhoiPortletBacking {
    private String answer;
    private String langid;
    private String tieudeguicauhoi;
    private String viewRender;

    private String hotenValue;
    private RichInputText txtHoten = new RichInputText();
    private String diachiValue;
    private RichInputText txtDiachi = new RichInputText();
    private String emailValue;
    private RichInputText txtEmail = new RichInputText();
    private String dienthoaiValue;
    private RichInputText txtDienthoai = new RichInputText();
    private String tieudeValue;
    private RichInputText txtTieude = new RichInputText();
    private String noidungValue;
    private RichInputText txtNoidung = new RichInputText();
    private String chudeSelectedValue;
    private SelectItem[] chudeElements = null;
    private RichSelectOneChoice chudeSelectChoice;
    private String ketqua = "";
    private RichOutputText rotKetqua = new RichOutputText();
    private RichPanelFormLayout formlayout;

    public GuicauhoiPortletBacking() {
        if (Utils.isPostBack()) {
            ketqua = "";
            rotKetqua.setVisible(false);
        }
    }

    public void beforePhase(PhaseEvent phaseEvent) {
        try {
            Integer reset =
                (Integer)Utils.getPortletRequest().getPortletSession().getAttribute("reset");
            if (reset != null && reset == 1) {
                Utils.getPortletRequest().getPortletSession().setAttribute("reset", 0);
                Utils.execute("Rollback");
                Utils.execute("CreateInsert");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getViewRender() {
        getLangid();
        getLinhvucChuDe();
        resetForm();
        return viewRender;
    }

    public void getLinhvucChuDe() {
        DCIteratorBinding linhvucIter =
            Utils.getIteratorBinding("HoidapLinhvucView1Iterator");
        ViewObject vo = linhvucIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcr = vc.createViewCriteriaRow();
        ViewCriteriaItem vci = vcr.ensureCriteriaItem("NgayHetHan");
        vci.setOperator(JboCompOper.OPER_ON_OR_AFTER);
        vci.setValue(Utils.getCurrentDate().dateValue().toString());
        vcr.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
        ViewCriteriaItem vci1 =
            vcr.ensureCriteriaItem("NgonNgu");
        vci1.setOperator(JboCompOper.OPER_EQ);
        vci1.setValue(langid);
        vc.add(vcr);
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    public SelectItem[] getChudeElements() {
        DCIteratorBinding linhvucIter =
            Utils.getIteratorBinding("HoidapLinhvucView1Iterator");
        int length = linhvucIter.getAllRowsInRange().length;
        chudeElements = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = linhvucIter.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("TenVn").toString());
            }
            chudeElements[i] = item;
        }
        return chudeElements;
    }
    
    private void resetForm(){
        hotenValue = "";
        txtHoten.setValue(null);
        diachiValue = "";
        txtDiachi.setValue(null);
        emailValue = "";
        txtEmail.setValue(null);
        dienthoaiValue = "";
        txtDienthoai.setValue(null);
        tieudeValue = "";
        txtTieude.setValue(null);
        noidungValue = "";
        txtNoidung.setValue(null);
        AdfFacesContext.getCurrentInstance().addPartialTarget(formlayout);
    }

    public void Insert(ActionEvent actionEvent) {
        if (capcha()) {
            try {
                DCIteratorBinding HoiDap =
                    Utils.getIteratorBinding("HoidapView1Iterator");

                HoiDap.getCurrentRow().setAttribute("Hoten", hotenValue);
                HoiDap.getCurrentRow().setAttribute("Diachi", diachiValue);
                HoiDap.getCurrentRow().setAttribute("Email", emailValue);
                HoiDap.getCurrentRow().setAttribute("Dienthoai",
                                                    dienthoaiValue);
                HoiDap.getCurrentRow().setAttribute("Tieude", tieudeValue);
                HoiDap.getCurrentRow().setAttribute("LinhvucId",
                                                    chudeSelectedValue);
                HoiDap.getCurrentRow().setAttribute("Noidung", noidungValue);
                HoiDap.getCurrentRow().setAttribute("NgayGui",
                                                    Utils.getCurrentDate().dateValue().toString());
                Utils.execute("CreateInsert");
                List lst = Utils.execute("Commit");
                if (lst == null) {
                    ketqua = "Gửi hỏi đáp chứng khoán thành công";
                    rotKetqua.setInlineStyle("color:blue;");
                    rotKetqua.setVisible(true);
                    resetForm();
                    this.setAnswer(null);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            if (this.getAnswer() != null) {
                ketqua = "Bạn nhập sai mã xác nhận. Yêu cầu nhập lại.";
                rotKetqua.setInlineStyle("color:red;");
                rotKetqua.setVisible(true);
                this.setAnswer(null);
            } else {
                ketqua = "Yêu cầu nhập mã xác nhận.";
                rotKetqua.setInlineStyle("color:red;");
                rotKetqua.setVisible(true);
            }
            Utils.getPortletRequest().getPortletSession().setAttribute("unreset", 2);
        }
    }

    public boolean capcha() {
        try {
            HttpServletRequest httpServletRequest =
                (HttpServletRequest)Utils.getPortletRequest().getAttribute("javax.servlet.request");
            if (httpServletRequest != null) {
                Captcha captcha =
                    (Captcha)httpServletRequest.getSession().getAttribute(Captcha.NAME);
                if (captcha != null && this.getAnswer() != null) {
                    if (captcha.isCorrect(this.getAnswer())) {
                        return true;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    private Locale getPortalLocale() {
        Locale portalLocale = Utils.getPortalLocale();
        return portalLocale;
    }

    private String Tieudeguicauhoi() {
        String tieudeguicauhoi = "Gửi câu hỏi";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("tieudeguicauhoi") != null)
                tieudeguicauhoi =
                        request.getParameter("tieudeguicauhoi").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tieudeguicauhoi;
    }

    public String getHoVaTenLabel() {
        String resource = Utils.getResource("HO_VA_TEN");
        return resource;
    }

    public String getDiaChiLabel() {
        String resource = Utils.getResource("DIA_CHI");
        return resource;
    }

    public String getDienThoaiLabel() {
        String resource = Utils.getResource("DIEN_THOAI");
        return resource;
    }

    public String getTieuDeLabel() {
        String resource = Utils.getResource("TIEU_DE");
        return resource;
    }

    public String getChuDeLabel() {
        String resource = Utils.getResource("CHU_DE");
        return resource;
    }

    public String getNoiDungCauHoiLabel() {
        String resource = Utils.getResource("NOI_DUNG_CAU_HOI");
        return resource;
    }

    public String getGuiCauHoiButton() {
        String resource = Utils.getResource("GUI_CAU_HOI");
        return resource;
    }

    public String getNhapLaiButton() {
        String resource = Utils.getResource("BO_QUA");
        return resource;
    }

    public String getNhapHoVaTen() {
        String resource = Utils.getResource("NHAP_HO_VA_TEN");
        return resource;
    }

    public String getNhapDiaChiMail() {
        String resource = Utils.getResource("NHAP_DIA_CHI_MAIL");
        return resource;
    }

    public String getNhapSaiDinhDangMail() {
        String resource = Utils.getResource("NHAP_SAI_DINH_DANG_MAIL");
        return resource;
    }

    public String getNhapTieuDe() {
        String resource = Utils.getResource("NHAP_TIEU_DE");
        return resource;
    }

    public String getChonChuDe() {
        String resource = Utils.getResource("CHON_CHU_DE");
        return resource;
    }

    public String getChonChuDeLable() {
        String resource = Utils.getResource("CHON_CHU_DE_LABLE");
        return resource;
    }

    public String getNhapNoiDung() {
        String resource = Utils.getResource("NHAP_NOI_DUNG");
        return resource;
    }

    public String getDoiMaKhac() {
        String resource = Utils.getResource("DOI_MA_KHAC");
        return resource;
    }

    public String getLangid() {
        langid = this.getPortalLocale().getLanguage().toString();
        return langid;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getAnswer() {
        return answer;
    }

    public void setTieudeguicauhoi(String tieudeguicauhoi) {
        this.tieudeguicauhoi = tieudeguicauhoi;
    }

    public String getTieudeguicauhoi() {
        tieudeguicauhoi = Tieudeguicauhoi();
        return tieudeguicauhoi;
    }

    public void setHotenValue(String hotenValue) {
        this.hotenValue = hotenValue;
    }

    public String getHotenValue() {
        return hotenValue;
    }

    public void setDiachiValue(String diachiValue) {
        this.diachiValue = diachiValue;
    }

    public String getDiachiValue() {
        return diachiValue;
    }

    public void setEmailValue(String emailValue) {
        this.emailValue = emailValue;
    }

    public String getEmailValue() {
        return emailValue;
    }

    public void setDienthoaiValue(String dienthoaiValue) {
        this.dienthoaiValue = dienthoaiValue;
    }

    public String getDienthoaiValue() {
        return dienthoaiValue;
    }

    public void setTieudeValue(String tieudeValue) {
        this.tieudeValue = tieudeValue;
    }

    public String getTieudeValue() {
        return tieudeValue;
    }

    public void setNoidungValue(String noidungValue) {
        this.noidungValue = noidungValue;
    }

    public String getNoidungValue() {
        return noidungValue;
    }

    public void chudeValueChanged(ValueChangeEvent valueChangeEvent) {
        chudeSelectedValue = valueChangeEvent.getNewValue().toString();
    }

    public void setChudeSelectedValue(String chudeSelectedValue) {
        this.chudeSelectedValue = chudeSelectedValue;
    }

    public String getChudeSelectedValue() {
        return chudeSelectedValue;
    }

    public void setChudeElements(SelectItem[] chudeElements) {
        this.chudeElements = chudeElements;
    }

    public void setChudeSelectChoice(RichSelectOneChoice chudeSelectChoice) {
        this.chudeSelectChoice = chudeSelectChoice;
    }

    public RichSelectOneChoice getChudeSelectChoice() {
        return chudeSelectChoice;
    }

    public void setKetqua(String ketqua) {
        this.ketqua = ketqua;
    }

    public String getKetqua() {
        return ketqua;
    }

    public void setRotKetqua(RichOutputText rotKetqua) {
        this.rotKetqua = rotKetqua;
    }

    public RichOutputText getRotKetqua() {
        return rotKetqua;
    }

    public void setTxtHoten(RichInputText txtHoten) {
        this.txtHoten = txtHoten;
    }

    public RichInputText getTxtHoten() {
        return txtHoten;
    }

    public void setTxtDiachi(RichInputText txtDiachi) {
        this.txtDiachi = txtDiachi;
    }

    public RichInputText getTxtDiachi() {
        return txtDiachi;
    }

    public void setTxtEmail(RichInputText txtEmail) {
        this.txtEmail = txtEmail;
    }

    public RichInputText getTxtEmail() {
        return txtEmail;
    }

    public void setTxtDienthoai(RichInputText txtDienthoai) {
        this.txtDienthoai = txtDienthoai;
    }

    public RichInputText getTxtDienthoai() {
        return txtDienthoai;
    }

    public void setTxtTieude(RichInputText txtTieude) {
        this.txtTieude = txtTieude;
    }

    public RichInputText getTxtTieude() {
        return txtTieude;
    }

    public void setTxtNoidung(RichInputText txtNoidung) {
        this.txtNoidung = txtNoidung;
    }

    public RichInputText getTxtNoidung() {
        return txtNoidung;
    }

    public void setFormlayout(RichPanelFormLayout formlayout) {
        this.formlayout = formlayout;
    }

    public RichPanelFormLayout getFormlayout() {
        return formlayout;
    }
}
